package com.kingre.et.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.servlet.ServletUtilities;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.context.SecurityContextHolder;

import com.kingre.et.chart.BarChart;
import com.kingre.et.chart.BarChart3D;
import com.kingre.et.chart.CylinderBarChart;
import com.kingre.et.chart.CylinderChart;
import com.kingre.et.model.School;
import com.kingre.et.model.Tdata;
import com.kingre.et.model.User;
import com.kingre.et.service.IDisSchoolReportService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.TextProvider;

public class DisSchoolReportAction extends ActionSupport{
    private static final long serialVersionUID = -2368525437963334496L;
    private static final Logger logger = Logger.getLogger(DisSchoolReportAction.class);
    @Autowired
    private IDisSchoolReportService disSchoolReportService;
    private Map<String, String> userResource;
    private Map<String, Double> avgData;
    private Map<String, List<Map<String, String>>> level;
    private Map<String, String> chartname;
    
    private Double avgAll;
    private List<Map<String, Double>> strength; 
    private List<Map<String, Double>> weakness; 
    private Integer id;
    
    private String titleName;

    public String report() throws IOException{
        School school;
        if(id == null || id == 0){
            User currentUser = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            school = disSchoolReportService.getSchoolByUser(currentUser.getId());
        }
        else{
            school = disSchoolReportService.getSchoolById(id);
        }
        userResource = disSchoolReportService.getSchoolUserResource(school);
        avgData = disSchoolReportService.getSchoolAVG(school);
        level = disSchoolReportService.getLevelPercent(school);
        chartname = disSchoolReportService.createChart(level, 
                new String[]{getText("level.1"),getText("level.2"),
                getText("level.3"),getText("level.4"),getText("level.5")},
                new String[]{getText("vd.title", new String[]{getText("chart.vd1")}),
                             getText("vd.title", new String[]{getText("chart.vd2")}),
                             getText("vd.title", new String[]{getText("chart.vd3")}),
                             getText("vd.title", new String[]{getText("chart.vd4")}),
                             getText("vd.title", new String[]{getText("chart.vd5")}),
                             getText("vd.title", new String[]{getText("chart.vd6")}),
                             getText("vd.title", new String[]{getText("chart.vd7")}),
                             getText("vd.title", new String[]{getText("chart.vd8")}),
                             getText("vd.title", new String[]{getText("chart.vd9")})});
        avgAll = disSchoolReportService.getAllAVG(school);
        strength = disSchoolReportService.getWeakness(avgData, false);
        weakness = disSchoolReportService.getWeakness(avgData, true);
        logger.debug("avgAll =" + avgAll);
        
        logger.debug("school =" + school);
        logger.debug("school.getSchoolType() =" + school.getSchoolType());
        if(school.getSchoolType() == 10)
        	titleName = this.getText("school.gaoxiao");
        else
        	titleName = this.getText("school.yuanxi");
        logger.debug("titleName =" + titleName);
        return SUCCESS;
    }
    
    public String createAndSave(Tdata t, Integer type) throws IOException{
        JFreeChart chart;
        if(type == 1)
            chart = BarChart.generateChart(
                    getText("chart.title"),
                    getText("chart.xlabel"),
                    getText("chart.ylabel"),
                    putChartDataset(t));
        else if(type == 2)
            chart = CylinderBarChart.generateChart(
                    getText("chart.title"),
                    getText("chart.xlabel"),
                    getText("chart.ylabel"),
                    putChartDataset(t));
        else if(type == 3)
            chart = BarChart3D.generateChart(
                    getText("chart.title"),
                    getText("chart.xlabel"),
                    getText("chart.ylabel"),
                    putChartDataset(t));
        else
            chart = CylinderChart.generateChart(
                    getText("chart.title"),
                    getText("chart.xlabel"),
                    getText("chart.ylabel"),
                    putChartDataset(t));
        StandardEntityCollection entityCollection = new StandardEntityCollection();
        ChartRenderingInfo info = new ChartRenderingInfo(entityCollection);
        return ServletUtilities.saveChartAsPNG(chart, 680, 350, info, null);
    }
    
    private List<Map<String, String>> putChartDataset(Tdata t){
        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        Map<String, String> map;
        if(t == null)
            return list;
        map = new HashMap<String, String>();
        map.put("vd", getText("chart.vd1"));
        map.put("value", String.valueOf(t.getV1()));
        list.add(map);
        map = new HashMap<String, String>();
        map.put("vd", getText("chart.vd2"));
        map.put("value", String.valueOf(t.getV2()));
        list.add(map);
        map = new HashMap<String, String>();
        map.put("vd", getText("chart.vd3"));
        map.put("value", String.valueOf(t.getV3()));
        list.add(map);
        map = new HashMap<String, String>();
        map.put("vd", getText("chart.vd4"));
        map.put("value", String.valueOf(t.getV4()));
        list.add(map);
        map = new HashMap<String, String>();
        map.put("vd", getText("chart.vd5"));
        map.put("value", String.valueOf(t.getV5()));
        list.add(map);
        map = new HashMap<String, String>();
        map.put("vd", getText("chart.vd6"));
        map.put("value", String.valueOf(t.getV6()));
        list.add(map);
        map = new HashMap<String, String>();
        map.put("vd", getText("chart.vd7"));
        map.put("value", String.valueOf(t.getV7()));
        list.add(map);
        map = new HashMap<String, String>();
        map.put("vd", getText("chart.vd8"));
        map.put("value", String.valueOf(t.getV8()));
        list.add(map);
        map = new HashMap<String, String>();
        map.put("vd", getText("chart.vd9"));
        map.put("value", String.valueOf(t.getV9()));
        list.add(map);
        return list;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }


    public Map<String, String> getUserResource() {
        return userResource;
    }

    public void setUserResource(Map<String, String> userResource) {
        this.userResource = userResource;
    }

    public Map<String, Double> getAvgData() {
        return avgData;
    }

    public void setAvgData(Map<String, Double> avgData) {
        this.avgData = avgData;
    }

    public Map<String, List<Map<String, String>>> getLevel() {
        return level;
    }

    public void setLevel(Map<String, List<Map<String, String>>> level) {
        this.level = level;
    }

    public Map<String, String> getChartname() {
        return chartname;
    }

    public void setChartname(Map<String, String> chartname) {
        this.chartname = chartname;
    }

    public List<Map<String, Double>> getStrength() {
        return strength;
    }

    public void setStrength(List<Map<String, Double>> strength) {
        this.strength = strength;
    }

    public List<Map<String, Double>> getWeakness() {
        return weakness;
    }

    public void setWeakness(List<Map<String, Double>> weakness) {
        this.weakness = weakness;
    }

    public Double getAvgAll() {
        return avgAll;
    }

    public void setAvgAll(Double avgAll) {
        this.avgAll = avgAll;
    }

	public String getTitleName() {
		return titleName;
	}

}
